package light.head.controller.mobile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.mobile.MobileGoodsApplyDAO;
import light.head.dao.mobile.MobileGoodsApplyItemDAO;
import light.head.dao.oa.AuditStatusDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.ApplyInfo;
import light.head.model.ApplyItem;
import light.head.model.AuditStatus;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 物品申请审核
 * @author yangguanghe @date 2014-6-10 @time 下午5:22:29
 *
 */
@Controller
@RequestMapping(Url.MOBILE_GOODSAPPLY_AUDIT)
public class MobileGoodsApplyAuditController extends SupporterController {

	private MobileGoodsApplyDAO goodsApplyDAO;
	private MobileGoodsApplyItemDAO applyItemDAO;
	private AuditStatusDAO auditStatusDAO;
	private OrganDAO organDAO;
	
	@RequestMapping(Url.INIT)
	public ModelAndView init (HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.MOBILE_GOODSAPPLY_AUDIT_INIT);
	}
	
	
	@RequestMapping(Url.QUERY)
	public ModelAndView list (HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String,String> map = createQueryMapByParmName(request, Parm.TITLE, Parm.ST, Parm.ET);
		
		goodsApplyDAO.findByMap_audit(organDAO.getSonCodeList(cu_organCode(request)), cu_roleCode(request), map, pb);
		
		if (null != pb.getResultList()) {
			
			List<Integer> acs = new ArrayList<Integer>();
			ApplyInfo applyInfo = null;
			
			for (Object obj : pb.getResultList()) {
				applyInfo = (ApplyInfo) obj;
				acs.add(applyInfo.getCode());
				// 审核状态
				if (null != applyInfo.getSort() && applyInfo.getSort().byteValue() == Sys.B_TWO) {
					applyInfo.setAuditStatusList(auditStatusDAO.findByBusinessMc(Sys.B_ONE, applyInfo.getCode()));
				}
			}
			List<ApplyItem> itemList = applyItemDAO.findByApplyCode(acs);
			
			for (Object obj : pb.getResultList()) {
				applyInfo = (ApplyInfo) obj;
				for (ApplyItem item : itemList) {
					if (applyInfo.getCode().intValue() == item.getAcode().intValue()) {
						applyInfo.addItem(item);
					}
				}
			}
			
		}
		
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.MOBILE_GOODSAPPLY_AUDIT_LIST);
	}
	
	// 审核
	@RequestMapping(Url.AUDIT)
	public void audit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		
		ApplyInfo info = goodsApplyDAO.get(Integer.parseInt(code));
		if (null == info) {
			print(response, Msg.NOT_FIND);
			return;
		}
		
		// 判断是否是固定资产，
		if (info.getSort().byteValue() != Sys.B_TWO) {
			return;
		}

		// 判断是否有审核权限
		List<AuditStatus> list = auditStatusDAO.findByBusinessMcRc(Sys.B_ONE,  info.getCode(), cu_roleCode(request));
		if (null == list || list.size() == 0) {
			print(response, Msg.NOT_AUDIT_PRIV);	// 没有审核权限
			return;
		}
		
		AuditStatus as = list.get(0);
		// 判断当前是否已经被审核
		if (null != as.getStatus() && as.getStatus().byteValue() == 1) {
			print(response, Msg.ALREADY_AUDIT);		// 已经审核通过
			return;
		}
		
		// 上一节点还未审核通过
		if (!auditStatusDAO.checkBeforeIsAudit(Sys.B_ONE,  info.getCode(), cu_roleCode(request))) {
			print(response, Msg.PRE_AUDIT_NO);	// 上一级未通过
			return;
		}
		
		// 可以审核
		as.setAu(cu_code(request));
		as.setAt(new Date());
		as.setStatus(Sys.B_ONE);
		if (RESULT.SUCCESS != auditStatusDAO.upd(as)) {
			throw new Exception("审核失败");
		}
			
		// 查询是否有下一个审核节点，有就将当前的审核状态添加到下一个审核状态的: beforeStatus 字段
		List<AuditStatus> nextList = auditStatusDAO.findByOcPcMcBusiness(as.getPc() + 1, info.getCode(), Sys.B_ONE);
		if (null != nextList && nextList.size() > 0) {
			AuditStatus nextas = nextList.get(0);
			nextas.setBeforeStatus(as.getStatus());
			if (RESULT.SUCCESS != auditStatusDAO.upd(nextas))
				throw new Exception("修改下一个审核节点信息失败");
		}
		
		print(response, Msg.SUCCESS);
	}


	@Autowired
	public void setGoodsApplyDAO(MobileGoodsApplyDAO goodsApplyDAO) {
		this.goodsApplyDAO = goodsApplyDAO;
	}

	@Autowired
	public void setApplyItemDAO(MobileGoodsApplyItemDAO applyItemDAO) {
		this.applyItemDAO = applyItemDAO;
	}

	@Autowired
	public void setAuditStatusDAO(AuditStatusDAO auditStatusDAO) {
		this.auditStatusDAO = auditStatusDAO;
	}


	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	
	
	
}
